﻿using System;
using System.Drawing;
using System.Runtime.Serialization;
using System.Web.UI.WebControls;
using ApplicationGenerator.BusinessModule.Elements;
using ApplicationGenerator.BusinessModule.MetaElements;
using ApplicationGenerator.CommonModule;
using ApplicationGenerator.Forms.Interfaces;
using ApplicationGenerator.Forms.Utilities;
using ApplicationGenerator.BusinessModule.Interfaces;
using System.ComponentModel;

namespace ApplicationGenerator.Forms.WebControls
{
    /// <summary>
    /// Kontrolka wyświetlająca struktury słownikowe
    /// </summary>
    [ToolboxBitmap(typeof(DropDownList))]
    [Serializable]
    public class DropDownListControl : BaseMappedControl, IInitializeControl, ILoadControl, ISaveControl
    {
        #region Fields

        /// <summary>
        /// Webowa kontrolka DropDownList
        /// </summary>
        [NonSerialized]
        private DropDownList _dropDownList;

        #endregion

        #region Properties
        /// <summary>
        /// Czy kontrolka ma być tylko do odczytu
        /// </summary>
        [Category("Rozszerzone dane")]
        [DisplayName("Tylko do odczytu")]
        [Description("Stwierdza, czy kontrolka ma być tylko do odczytu.")]
        public bool IsReadOnly
        {
            get;
            set;
        }

        /// <summary>
        /// Kontrolka webowa powiązana z daną kontrolką
        /// </summary>
        [Browsable(false)]
        public override WebControl Control
        {
            get
            {
                if (_dropDownList == null)
                {
                    _dropDownList = new DropDownList();
                }

                return _dropDownList;
            }
        }
        #endregion

        #region IInitializeControl Members
        /// <summary>
        /// Inicjalizuje kontrolkę
        /// </summary>
        public void Initialize()
        {
            string[] pathParts = Path.Split(new char[] { '.' });
            if (pathParts.Length > 0)
            {
                string dictionaryName = Source.MetaFieldCollection[pathParts[pathParts.Length - 1]].FieldType.Name;
                IMetaDictionary metaDictionary = (new FacadeFactory()).CreateMetaModelFacade().MetaDictionaryCollection[dictionaryName];
                string metaDictionaryValue = null;
                foreach (object metaDictionaryItem in metaDictionary.MetaDictionaryItems)
                {
                    metaDictionaryValue = metaDictionaryItem as string;
                    if (metaDictionaryValue != null)
                    {
                        _dropDownList.Items.Add(new ListItem(metaDictionaryValue));
                    }
                }
            }

            _dropDownList.Enabled = !IsReadOnly;
        }

        #endregion

        #region ILoadControl Members
        /// <summary>
        /// Ładuje kontrolkę wartością z modelu
        /// </summary>
        /// <param name="modelElement">Model</param>
        public void Load(IModel modelElement)
        {
            if (modelElement == null || modelElement.ModelInstance == null)
            {
                return;
            }
            string[] pathParts = Path.Split(new char[] { '.' });
            _dropDownList.SelectedValue = modelElement.GetDictionaryValue(pathParts[pathParts.Length - 1]);
        }

        #endregion

        #region ISaveControl Members
        /// <summary>
        /// Zapisuje aktualną wartość kontrolki do modelu
        /// </summary>
        /// <param name="modelElement">Model</param>
        void ISaveControl.Save(IModel modelElement)
        {
            if (modelElement != null)
            {
                string[] pathParts = Path.Split(new char[] { '.' });
                modelElement.SetDictionaryValue(pathParts[pathParts.Length - 1], _dropDownList.SelectedValue);
            }
        }

        #endregion
    }
}
